const { segment } = require("icqq")
const { bot } = require("../index")

let Config = new class {
    constructor() {
        this.GroupId = ['', ];//需要管理的群
        this.comment = ["",]//入群申请验证消息答案（如果答案不在这里面，则不处理）
        this.msg=[
            ``,
        ]//入群欢迎词
    }
    async sleep (ms) {
        return new Promise((resolve) => setTimeout(resolve, ms))
    }
}

bot.on('notice.group.decrease', async (event) => {
    const { group_id, operator_id, user_id } = event;
    if (Config.GroupId.includes(group_id)) {
        if (operator_id == user_id) {
            let { nickname } = await bot.getStrangerInfo(user_id)
            let msgtext = [
                `退群通知：`,
                `${nickname}(${user_id})以退出本群`,
            ]
            bot.sendGroupMsg(group_id, msgtext);
        }
        else if (operator_id != user_id) {

            let { nickname } = await bot.getStrangerInfo(user_id)
            let member = bot.pickGroup(group_id).pickMember(operator_id);
            let memebrName = member.info.card || member.info.nickname
            console.log(memebrName)
            let msgtext = [
                `退群通知：`,
                `${nickname}(${user_id})以被管理${memebrName}(${operator_id})踢出本群`,
            ]
            bot.sendGroupMsg(group_id, msgtext);

        }
    }
});

bot.on('notice.group.increase', async (event) => {
    const { group_id, user_id } = event;
    if (Config.GroupId.includes(group_id)) {
        let reply_msg = [
            segment.at(user_id),
            Config.msg,
        ]
        bot.sendGroupMsg(group_id, reply_msg);
    }
})

bot.on('request.group.add', async (event) => {
    console.log(event);
    const { group_id, comment } = event;
    if (Config.GroupId.includes(group_id)) {
        let comment_split = comment.split("答案：")
        let cm = (comment_split && comment_split[1]) ? comment_split[1].replace(/[\r\n]/g, "") : ""
        if (Config.comment.includes(cm)) {
            event.approve(true)
        }
    }
})